package com.neusoft.bizcore.mom.messaging.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.mom.bean.MessageBean;
import com.neusoft.bizcore.mom.messaging.ProducerService;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component("kafkaProducerService")
@ConditionalOnClass(KafkaTemplate.class)
public class KafkaProducerServiceImpl implements ProducerService {

    @Autowired
    private KafkaTemplate<Object, MessageBean> kafkaTemplate;

    @Autowired
    private KafkaTemplate<Object, String> kafkaJsonTemplate;

    @Override
    public void send(final String topic, final MessageBean message) {
        this.kafkaTemplate.send(topic, message);
        if (KafkaProducerServiceImpl.log.isDebugEnabled()) {
            KafkaProducerServiceImpl.log.debug("send to kafka topic {} a message {}", topic, message.getMessage());
        } else if (KafkaProducerServiceImpl.log.isInfoEnabled()) {
            KafkaProducerServiceImpl.log.info("send to kafka topic {} a message", topic);
        }
    }

    @Override
    public void send(String topic, String value) {
        this.kafkaJsonTemplate.send(topic, value);
        if (KafkaProducerServiceImpl.log.isDebugEnabled()) {
            KafkaProducerServiceImpl.log.debug("send to kafka topic {} a message {}", topic, value);
        }
    }

    @Override
    public void send(String topic, String key, String value) {
        this.kafkaJsonTemplate.send(topic, key, value);
        if (KafkaProducerServiceImpl.log.isDebugEnabled()) {
            KafkaProducerServiceImpl.log.debug("send to kafka topic {} a message {}", topic, value);
        }
    }
}
